﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Module.Stat.Model;

namespace Cn.Cellula.Website.Admin.Module.Stat {
    public partial class EditStat : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        String EOKey = "";
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm();
                }
            }
        }

        private void InitForm() {
            IList<ModTStatChartEO> list = biz.LoadAll<ModTStatChartEO>();
            this.ddl_Chart.DataTextField = "ChartName";
            this.ddl_Chart.DataValueField = "ChartKey";
            this.ddl_Chart.DataSource = list;
            this.ddl_Chart.DataBind();

        }

        private void FillForm() {
            ModTStatEO eo = biz.Load<ModTStatEO>(EOKey);
            this.txt_StatName.Text = eo.StatName;
            this.txt_StatSubName.Text = eo.StatSubName;
            this.txt_TrendName.Text = eo.TrendName;
            this.txt_TrendValue.Text = eo.TrendValue+"";
            this.txt_XName.Text = eo.XName;
            this.txt_YName.Text = eo.YName;
            this.ddl_Chart.SelectedValue = eo.ChartKey+"";
            this.radl_IsShowValues.SelectedValue = eo.IfShowValues + "";
            this.txt_NumberPrefix.Text = eo.NumberPrefix;

            this.tadl_SortOption.SelectedValue = eo.OrderOption;
            this.txt_OrderField.Text = eo.OrderField;
            this.txt_DataKeyField.Text = eo.DataKeyField;
            this.txt_DataTextField.Text = eo.DataTextField;
            this.txt_DataValueField.Text = eo.DataValueField;
            this.txt_SrcName.Text = eo.SrcName;
            this.txt_DataKeyCaption.Text = eo.DataKeyCaption;
            this.txt_DataTextCaption.Text = eo.DataTextCaption;
            this.txt_DataValueCaption.Text = eo.DataValueCaption;

        }

        protected void btn_save_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(EOKey)) {
                ModTStatEO eo = new ModTStatEO();
                eo.ChartKey = Convert.ToInt32(this.ddl_Chart.SelectedValue);
                eo.CreateDt = DateTime.Now;
                eo.LastDt = DateTime.Now;
                eo.Operator = this.Actor.ID;
                eo.StatName = this.txt_StatName.Text;
                eo.TrendName = this.txt_TrendName.Text;
                eo.StatSubName = this.txt_StatSubName.Text;
                eo.NumberPrefix = this.txt_NumberPrefix.Text;
                eo.IfShowValues = Convert.ToInt32(this.radl_IsShowValues.SelectedValue);
                try {
                    eo.TrendValue = Convert.ToDouble(this.txt_TrendValue.Text);
                } catch { }
                eo.XName = this.txt_XName.Text;
                eo.YName = this.txt_YName.Text;

                eo.DataValueField = this.txt_DataValueField.Text;
                eo.DataTextField = this.txt_DataTextField.Text;
                eo.DataKeyField = this.txt_DataKeyField.Text;
                eo.OrderField = this.txt_OrderField.Text;
                eo.SrcName = this.txt_SrcName.Text;
                eo.OrderOption = this.tadl_SortOption.SelectedValue;
                eo.DataValueCaption = this.txt_DataValueCaption.Text;
                eo.DataTextCaption = this.txt_DataTextCaption.Text;
                eo.DataKeyCaption = this.txt_DataKeyCaption.Text;
                eo.OwnerKey = this.OwnerKey;
                
                Object key = biz.Save<ModTStatEO>(eo);
                Response.Redirect("List.aspx?sid=" + key.ToString());
            } else { //update
                ModTStatEO eo = biz.Load<ModTStatEO>(EOKey);
                eo.ChartKey = Convert.ToInt32(this.ddl_Chart.SelectedValue);
                eo.LastDt = DateTime.Now;
                eo.Operator = this.Actor.ID;
                eo.StatName = this.txt_StatName.Text;
                eo.TrendName = this.txt_TrendName.Text;
                eo.StatSubName = this.txt_StatSubName.Text;
                eo.NumberPrefix = this.txt_NumberPrefix.Text;
                eo.IfShowValues = Convert.ToInt32(this.radl_IsShowValues.SelectedValue);
                try {
                    eo.TrendValue = Convert.ToDouble(this.txt_TrendValue.Text);
                } catch { }
                eo.XName = this.txt_XName.Text;
                eo.YName = this.txt_YName.Text;

                eo.DataValueField = this.txt_DataValueField.Text;
                eo.DataTextField = this.txt_DataTextField.Text;
                eo.DataKeyField = this.txt_DataKeyField.Text;
                eo.OrderField = this.txt_OrderField.Text;
                eo.SrcName = this.txt_SrcName.Text;
                eo.OrderOption = this.tadl_SortOption.SelectedValue;
                eo.DataValueCaption = this.txt_DataValueCaption.Text;
                eo.DataTextCaption = this.txt_DataTextCaption.Text;
                eo.DataKeyCaption = this.txt_DataKeyCaption.Text;

                biz.Update<ModTStatEO>(eo);
                Response.Redirect("List.aspx?sid=" + eo.StatKey);

            }
        }
    }
}